# -*- coding: utf-8 -*-
'''
Created on 2014-12-11
@chapter: 2.2
@name: 简单迭代法
@author: revol
'''
from solve import Solve
import math

class Picard(Solve):
    '''
    简单迭代法
    '''


    def __init__(self, data=[]):
        Solve.__init__(self, data)
        self.x0=3
        if data:
            self.calc(data)
        self._name=u'简单迭代法'
        self._description=u'简单迭代法又称不动点迭代法、Picard 迭代法。\n它是用固定公式\
反复校正根的近似值，使之逐步精确最后得到结果。\n内建函数f(x)=2x-lg(x)-7 , 初值x0=3 \n\
迭代函数p(x)=1/2(lg(x)+7)'
        self._paramenters=[{'name':'accuracy','type':self.typeFLOAT}]
        
    def p(self,x):
        return 0.5*(math.log10(x)+7)
    
    def calc(self, data):
        Solve.calc(self, data)
        accuracy=data['accuracy']
        x=self.x0        
        x1=self.p(x)
        while(abs(x-x1)>accuracy):
            x=self.p(x1)
            x1=self.p(x)
        self._result=x
        return x
    
    def getOutput(self):
        out=u"参数：\n\t精度： %f \n近似根： %f \n" %(self._data['accuracy'],self._result)
        return out

if __name__ == '__main__':
    data={'accuracy':0.0005}
    test=Picard(data)
    print test.getOutput()
    print test.getResult()
    print test.getDescription()
    print test.getParamenters()